﻿namespace Ex03.GarageLogic
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    internal class Truck : Vehicle
    {
        #region Messages
        private const string k_StringNotAllowedCapacity = "{0} is not an allowed container capacity.";
        private const string k_StringNotAllowedMaxWeight = "{0} is not an allowed max carrying weight.";
        private const string k_StringNotAllowedCarriesHazardouseInd = "{0} is not a legal value (Y / N).";
        private const string k_MaxAllowedCarryingWeight = "Max allowed carrying weight: ";
        private const string k_ContainerCapacity = "Container capacity: ";
        private const string k_CarriesHazardousMaterial = "Carries hazardous material (Y - yes, N - no): ";
        private const string k_DisplayMaxAllowedCarryingWeight = "Max allowed carrying weight: ";
        private const string k_DisplayContainerCapacity = "Container capacity: ";
        private const string k_DisplayCarriesHazardousMaterial = "Carries hazardous material: ";
        #endregion

        #region Data Members
        private float m_MaxAllowedCarryingWeight;
        private float m_ContainerCapacity;
        private bool m_CarriesHazardousMaterial;
        #endregion

        #region Constructors
        public Truck(string i_LicenseNumber, Engine i_Engine, int i_NumOfWheels, float i_MaxPSI)
            : base(i_LicenseNumber, i_Engine, i_NumOfWheels, i_MaxPSI)
        {
        }
        #endregion

        #region Properties
        public string MaxAllowedCarryingWeight
        {
            get { return m_MaxAllowedCarryingWeight.ToString(); }
            set
            {
                float floatMaxAllowedCarryingWeight = 0f;
                bool goodInput = false;
                goodInput = float.TryParse(value, out floatMaxAllowedCarryingWeight);

                if (goodInput == false || floatMaxAllowedCarryingWeight < 0)
                {
                    throw new FormatException(string.Format(k_StringNotAllowedMaxWeight, value));
                }
                else
                {
                    m_MaxAllowedCarryingWeight = floatMaxAllowedCarryingWeight;
                }
            }
        }

        public string ContainerCapacity 
        {
            get { return m_ContainerCapacity.ToString(); }
            set
            {
                float floatContainerCapacity = 0f;
                bool goodInput = false;
                goodInput = float.TryParse(value, out floatContainerCapacity);

                if (goodInput == false || floatContainerCapacity < 0 || floatContainerCapacity > m_MaxAllowedCarryingWeight)
                {
                    throw new FormatException(string.Format(k_StringNotAllowedCapacity, value));
                }
                else
                {
                    m_ContainerCapacity = floatContainerCapacity;
                }
            }
        }

        public string CarriesHazardousMaterial 
        {
            get { return m_CarriesHazardousMaterial.ToString(); }
            set
            {
                if (value.Trim().ToLower() == "y")
                {
                    m_CarriesHazardousMaterial = true;
                }
                else if (value.Trim().ToLower() == "n")
                {
                    m_CarriesHazardousMaterial = false;
                }
                else
                {
                    throw new ArgumentException(string.Format(k_StringNotAllowedCarriesHazardouseInd, value));
                }
            }
        }
        #endregion

        #region Public Methods
        public override void GetAllDetails(ref List<string> io_ListOfVehicleDetails)
        {
            base.GetAllDetails(ref io_ListOfVehicleDetails);
            io_ListOfVehicleDetails.Add(string.Format(GarageLogic.s_PrintParameter, k_DisplayMaxAllowedCarryingWeight, MaxAllowedCarryingWeight));
            io_ListOfVehicleDetails.Add(string.Format(GarageLogic.s_PrintParameter, k_DisplayContainerCapacity, ContainerCapacity));
            io_ListOfVehicleDetails.Add(string.Format(GarageLogic.s_PrintParameter, k_DisplayCarriesHazardousMaterial, CarriesHazardousMaterial));
            base.GetEngineAndWheelsDetails(ref io_ListOfVehicleDetails);
        }

        public override void GetClassParametersNeededToCreate(ref List<string> io_ListOfParameters)
        {
            base.GetClassParametersNeededToCreate(ref io_ListOfParameters);

            io_ListOfParameters.Add(k_MaxAllowedCarryingWeight);
            io_ListOfParameters.Add(k_ContainerCapacity);
            io_ListOfParameters.Add(k_CarriesHazardousMaterial);
        }

        public override void SetClassByParameters(ref Dictionary<string, string> io_Parameters)
        {
            base.SetClassByParameters(ref io_Parameters);

            if (GarageLogic.KeyFoundInDictionaryAndNotEmptyOrNull(io_Parameters, k_MaxAllowedCarryingWeight))
            {
                MaxAllowedCarryingWeight = io_Parameters[k_MaxAllowedCarryingWeight];
                io_Parameters[k_MaxAllowedCarryingWeight] = string.Empty;
            }

            if (GarageLogic.KeyFoundInDictionaryAndNotEmptyOrNull(io_Parameters, k_ContainerCapacity))
            {
                ContainerCapacity = io_Parameters[k_ContainerCapacity];
                io_Parameters[k_ContainerCapacity] = string.Empty;
            }

            if (GarageLogic.KeyFoundInDictionaryAndNotEmptyOrNull(io_Parameters, k_CarriesHazardousMaterial))
            {
                CarriesHazardousMaterial = io_Parameters[k_CarriesHazardousMaterial];
                io_Parameters[k_CarriesHazardousMaterial] = string.Empty;
            }
        }
        #endregion
    }
}